# 面向对象过程:
# 定义类:类名(Register),属性(手机号,密码,验证码,获取验证码......),方法(输入......,点击......)
# 实例:通过类创建对象,类名()

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class Register:
    # 属性
    def __init__(self, driver):
        """元素定位:属性"""
        # 定义驱动的属性
        self.driver = driver
        # 定义元素属性
        self.phone = (By.ID, "phone")
        self.password = (By.ID, "password")
        self.verifycode = (By.ID, "verifycode")
        self.get_phone_code = (By.ID, "get_phone_code")
        self.phone_code = (By.ID, "phone_code")
        self.reg_btn = (By.CLASS_NAME, "lg-btn")

    # 方法
    def input_phone(self, phone):
        """输入手机号"""
        # self.driver.find_element(*self.phone).send_keys(phone)
        # 显式等待
        element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.phone))
        # 清空默认值
        element.clear()
        element.send_keys(phone)

    def input_password(self, password):
        """输入密码"""
        # self.driver.find_element(*self.password).send_keys(password)
        # 显式等待
        element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.password))
        # 清空默认值
        element.clear()
        element.send_keys(password)

    def input_verifycode(self, verifycode="8888"):
        """输入图片验证码"""
        # self.driver.find_element(*self.verifycode).send_keys(verifycode)
        # 显式等待
        element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.verifycode))
        # 清空默认值
        element.clear()
        element.send_keys(verifycode)

    def click_get_phone_code(self):
        """点击获取短信验证码"""
        # self.driver.find_element(*self.get_phone_code).click()
        # 显式等待
        element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.get_phone_code))
        element.click()

    def input_phone_code(self, phone_code="666666"):
        """输入短信验证码"""
        # self.driver.find_element(*self.phone_code).send_keys(phone_code)
        # 显式等待
        element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.phone_code))
        # 清空默认值
        element.clear()
        element.send_keys(phone_code)

    def click_reg_btn(self):
        """点击注册按钮"""
        # self.driver.find_element(*self.reg_btn).click()
        # 显式等待
        element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.reg_btn))
        element.click()

    def register(self, phone, password, verifycode="8888", phone_code="666666"):
        """注册流程方法"""
        self.input_phone(phone)
        self.input_password(password)
        self.input_verifycode(verifycode)
        self.click_get_phone_code()
        self.input_phone_code(phone_code)
        self.click_reg_btn()
